package org.fujene.memoryblock.structure;

import java.nio.charset.Charset;

public class VariableLengthString extends SerializableNumber<String>{
	private static final long serialVersionUID = -9059327767337472733L;
	private String value;
	private byte[] serialized;

	// argument has no mean
	public VariableLengthString(int len){
		super(0);
	}

	public VariableLengthString(byte[] b, int len){
		super(b, 0); // Do nothing
		serialized = b;
		value = new String(b, Charset.forName("UTF-8"));
	}

	@Override
	public byte[] toByte(){
		return serialized;
	}

	public int byteLength(){
		return serialized.length;
	}

	@Override
	public String fromByte(byte[] b){
		serialized = b;
		value = new String(b, Charset.forName("UTF-8"));
		return value;
	}

	@Override
	public String getValue(){
		return value;
	}

	@Override
	public void setValue(String val){
		value = val;
		serialized = val.getBytes(Charset.forName("UTF-8"));
	}

	@Override
	public double doubleValue(){
		return intValue();
	}

	@Override
	public float floatValue(){
		return intValue();
	}

	@Override
	public int intValue(){
		// TODO Auto-generated method stub
		return serialized.length;
	}

	@Override
	public long longValue(){
		return intValue();
	}
}
